<?php

namespace app\api\controller;

use app\common\BaseHelper;

use app\admin\model\Userprojectfile as UserprojectfileMdl;
use app\admin\model\User;
use app\admin\model\Member;
use app\admin\model\Userfolder;
use app\admin\model\Libraryfolder;
use app\admin\model\Image;

use think\Db;
use think\Exception;
use think\Request;

class Userprojectfile extends BaseController
{

    /**
     * 列表
     */
    public function getFiles()
    {
        $param = self::getHttpParam();

        $start = 0;
        $length = 20;

        if (empty($param->userId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'userId 不能为空');
        }
        if (empty($param->projectId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'projectId 不能为空');
        }
        if (!empty($param->start)) {
            $start = $param->start;
        }
        if (!empty($param->length)) {
            $length = $param->length;
        }

        $projectId = $param->projectId;
        $where = [
            'projectId' => $projectId
        ];

        $model = new UserprojectfileMdl();
        $fields = '*';
        $order = 'createDate DESC';
        $result = $model->where($where)->field($fields)->limit($start, $length)->order($order)->select();
        $total = $model->where($where)->count();
        if (!$result) {
            return show(config('status.ERROR_STATUS'), self::NOT_DATA, '没有相关数据');
        }

        foreach ($result as $item) {
            // 上传者信息
            $userInfo = User::where(['id' => $item['uploader']])->field('id as userId, memberId, nickName as name, avatarUrl as avatar')->find();
            if($userInfo['memberId']){
                $userInfo = Member::where(['id' => $userInfo['memberId']])->field('name, avatar, userId')->find();
            }
            $item['userInfo'] = $userInfo;
        }

        return showTotal(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $result, $total);
    }

    /**
     * 上传文件
     */
    public function uploadFile()
    {
        $param = self::getHttpParam();

        if (empty($param->userId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'userId 不能为空');
        }
        if (empty($param->projectId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, '项目ID(projectId) 不能为空');
        }
        if (empty($param->name)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, '文件名称(name) 不能为空');
        }

        Db::startTrans();
        try {
            $data = (array)$param;
            $data['fileName'] = $data['name'];
            if($data['fileUrl']){
                @$data['fileType'] = pathinfo($data['fileUrl'], PATHINFO_EXTENSION);
            }

            $model = new UserprojectfileMdl();

            $data['id'] = BaseHelper::getUUID();
            $data['createDate'] = time();
            $data['uploader'] = $param->userId;

            $result = $model->save($data);
            if($result === false){
                Db::rollback();
                return show(config('status.ERROR_STATUS'), '上传失败', $model->getError());
            }

            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            return show(config('status.ERROR_STATUS'), '上传失败', $e->getMessage());
        }

        return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, []);
    }

    /**
     * 重命名
     */
    public function rename()
    {
        $param = self::getHttpParam();

        if (empty($param->fileId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'fileId 不能为空');
        }
        if (empty($param->fileName)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'fileName 不能为空');
        }

        $where = [
            'id' => $param->fileId
        ];
        $model = new UserprojectfileMdl();
        $result= $model->where($where)->update(['fileName' => $param->fileName]);

        if ($result === false) {
            return show(config('status.ERROR_STATUS'), self::NOT_DATA, '重命名失败');
        }

        return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $result);
    }

    /**
     * 删除
     */
    public function delFile()
    {
        $param = self::getHttpParam();

        if (empty($param->fileId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'fileId 不能为空');
        }

        $where = [
            'id' => $param->fileId
        ];
        $model = new UserprojectfileMdl();
        $result = $model->where($where)->delete();
        if ($result === false) {
            return show(config('status.ERROR_STATUS'), self::NOT_DATA, '删除失败');
        }

        // Query执行后的操作
        //$model->_after_delete($param->id); // 不能删除七牛云图片，因为该地址有可能以被别人下载到云盘

        return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $result);
    }

}